<?php 

/**
* 问题类
*/
namespace Home\Controller;
use Think\Controller;
class QueController extends Controller
{	

	function index()
	{		
			$count= M('question')->count();
	        $Page       = new \Think\Page($count,4);// 实例化分页类 传入总记录数和每页显示的记录数(25)
	        $show       = $Page->show();// 分页显示输
	        $this->assign('show',$show);

	        //得到指定的问题ID，再从数据库中取得其数据
			$id = I('get.id');
			$sql = "select * from question where id = $id";
			$res =M()->query($sql);
			$this->assign('res',$res);

			//分配分页到模版
			$sql = "select u.face,u.nikename,a.id,a.content,a.status,a.zan,a.create_time  from user u  join answer a on u.id=a.uid where qid = $id limit ".$Page->firstRow.','.$Page->listRows;
			$ans = M()->query($sql);
			$this->assign('ans',$ans);

			$sql ='select title,view_num,id from question order by view_num desc limit 15';
    		$hot = M()->query($sql);
    		$this->assign('hot',$hot);

        	$sql ='select title,answer_num,id from question order by answer_num desc limit 15';
        	$hot_ans = M()->query($sql);
        	$this->assign('hot_ans',$hot_ans);


			//记录问题的被浏览次数
			$sql="update question set view_num = view_num+1 where id = $id ";
			$row = M()->execute($sql);
			if ($row) {
				$this->display();
			}

		    
	}

	function add()
	{	 
		if (isset($_SESSION['uid'])) {	
			$sql = "select * from vercode order by rand() limit 1";
			$res = M()->query($sql);
			$_SESSION['answer']=$res[0]['answer'];
			$sql = "select * from category";
			$res1 = M()->query($sql);
			$this->assign('res1',$res1);
			$this->assign('res',$res);
		    $this->display();
		}else{
			$this->success('请重新登录！',U('home/login/index'));
		}

	}

	function getdata()
	{
		$data = I('post.');
		$queinfo['uid'] = $_SESSION['uid'];
		$queinfo['cid'] = $data['class'];
		$queinfo['title'] = $data['title'];
		$queinfo['content'] = $data['content'];
		$queinfo['kiss'] = $data['experience'];
		$queinfo['create_time'] = time();

		$res =M('question')->add($queinfo);

		if ($res) {
			$arr = array('error' =>1 ,'info'=>'发表成功' ,'id'=>$res);
			echo json_encode($arr);
			// $this->success('发表成功',U('home/que/index'));
		}else{
			$arr = array('error' =>0 ,'info'=>'发表失败' );
			echo json_encode($arr);
			// $this->error('发表失败',U('home/que/add'));
		}
		

	
	}

	public function upload()
	{
		    $upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath  =     './Uploads/que/'; // 设置附件上传根目录
			$upload->savePath  =     ''; // 设置附件上传（子）目录
			// 上传文件 
			$info   =   $upload->upload();
				if(!$info) {// 上传错误提示错误信息
				    $this->error($upload->getError());
				}else{// 上传成功
				    $arr = array(
				    	'code' =>0,
				    	'msg' => '上传成功',
				    	'data'=> array(
				    		'src' =>__ROOT__."/Uploads/que/".$info['file']['savepath'].$info['file']['savename'], 
	    					'title'=>'图片文件'
				    		)
				    	);
				    echo json_encode($arr);
				}
	}

	public function getanswer()
	{	
					
		if (isset($_SESSION['uid'])) {	
			$ans = I('post.');
			if (!empty($ans['content'])) {
				$info = $ans;
				$info['uid']= $_SESSION['uid'];
				$info['create_time']= time();
				$res = M('answer')->add($info);
				if ($res) {
					$sql ="update question set answer_num = answer_num+1 where id ={$ans['qid']}";
					$res = M()->execute($sql);
				 	$arr = array('error' =>1 ,'info'=>'发表成功' );
					echo json_encode($arr);
				 } 
			}else{
				$arr = array('error' =>2 ,'info'=>'发表内容不能为空！' );
				echo json_encode($arr);
			}
			
		}else{
			$arr = array('error' =>0 ,'info'=>'请先登录' );
			echo json_encode($arr);
		}
		
		
	}

	public function zan()
	{
		$data = current(I('post.'));
		$aid = $data[0];
		$uid = $data[1];

		if (empty($uid)) {
			$arr = array('error' =>0 ,'info'=>'没登陆不能点赞');
			echo json_encode($arr);
		}else{
			$sql = "select * from zan where uid = '{$uid}' and aid = '{$aid}'";
			$row = M()->query($sql);
			if ($row) {
				$id = $row[0]['id'];
				$sql ="delete from zan where id = '{$id}'";
				$del =M()->execute($sql);
				if($del){
					$sql = "update answer set zan = zan-1 where id = $aid";
					$update =M()->execute($sql);
					$arr = array('error' =>1,'info'=>'取消点赞');
					echo json_encode($arr);
				}	
			}else{
				$data['aid'] = $aid;
				$data['uid'] = $uid;
				$res = M('zan')->add($data);
				if ($res) {
					$sql = "update answer set zan = zan+1 where id = $aid";
					$update =M()->execute($sql);
					$arr = array('error' =>2,'info'=>'点赞成功');
					echo json_encode($arr);
				}
				
			}

		}

		
	}


}












 ?>